﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.项目流程.Service;
using DevExpress.XtraGrid.Demos.util;
using System.Net;
using DXApplicationTangche.DTO;
using DevExpress.XtraGrid.Views.Grid;

namespace DXApplicationTangche.UC.项目流程
{
    public partial class Form未完成项目一览 : DevExpress.XtraEditors.XtraForm
    {
        private List<OrderWholeProjectDTO> orderWholeProjectDTOs;
        private List<OrderTaskDTO> orderTaskDTOs;

        public Form未完成项目一览()
        {
            InitializeComponent();
            this.orderWholeProjectDTOs = 流程Service.getDoneFalseOrderWholeProjects();
            this.gridControl未完成项目一览.DataSource = this.orderWholeProjectDTOs;
        }

        private void gridView未完成项目一览_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            String orderId = 
                FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView1, this.advBandedGridView1.FocusedRowHandle, "orderId");
            //点击后给下方甘特图赋值
            this.orderTaskDTOs =
                流程Service.findOrderTaskDTOsByOrderTaskQueryDTO(new OrderTaskQueryDTO(orderId));
            this.treeList一览.DataSource = this.orderTaskDTOs;
            this.treeList一览.Refresh();
            this.treeList一览.ExpandAll();
            //成果物赋值
            this.gridControl成果物.DataSource = 流程Service.getProjectAchievementByOrderId(orderId);
        }
        private void treeList一览_RowClick(object sender, DevExpress.XtraTreeList.RowClickEventArgs e)
        {
            new Form流程门户(
                this.treeList一览.FocusedNode["id"].ToString(),
                this.treeList一览.FocusedNode["wbsTemplateId"].ToString(),
                this.treeList一览.FocusedNode["orderId"].ToString(),
                this.treeList一览.FocusedNode["code"].ToString()
                ).ShowDialog();
        }

        private void treeList一览_CustomUnboundColumnData(object sender, DevExpress.XtraTreeList.TreeListCustomColumnDataEventArgs e)
        {
            if (e.Column.Name == "treeListColumn实际完成时间" && e.IsGetData)
            {
                e.Value = this.orderTaskDTOs[e.NodeID].actualFinishDate == DateTime.MinValue ? "" : this.orderTaskDTOs[e.NodeID].actualFinishDate.ToString();
            }
        }

        private void 下载成果物文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String filePath = FunctionHelper.getTileView(this.tileView成果物,this.tileView成果物.FocusedRowHandle, "filePath");
            if (String.IsNullOrEmpty(filePath))
            {
                MessageBox.Show("暂无文件");
            }
            else
            {
                FolderBrowserDialog sFD = new FolderBrowserDialog();
                DialogResult rdr = sFD.ShowDialog();
                string selectFileName = sFD.SelectedPath;
                if (rdr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(selectFileName))
                {
                    try
                    {
                        //通过url下载文件
                        WebClient wClient = new WebClient();
                        wClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        wClient.DownloadFile(URL.图片url + URL.项目成果物文件路径 + filePath, selectFileName + "\\" + filePath);
                        MessageBox.Show("保存成功");
                    }
                    catch
                    {
                        MessageBox.Show("保存失败");
                    }
                }
            }
        }
        private void treeList一览_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column.Name.Equals("treeListColumn完成标识"))
            {
                if (false.Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.White;
                    e.Appearance.ForeColor = Color.Black;
                }
                else
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }
        //红旗
        private void treeList一览_CustomDrawNodeImages(object sender, DevExpress.XtraTreeList.CustomDrawNodeImagesEventArgs e)
        {
            //根据索引取done
            if (this.orderTaskDTOs[e.Node.Id].done)
            {
                e.SelectImageIndex = 0;
            }
            else
            {
                e.SelectImageIndex = 1;
            }
        }
    }
}